package com.xiaomili.halfsugar.fragment;


import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.FragmentAdapter;
import com.xiaomili.halfsugar.app.IApp;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 *
 */
public class CommunityFragment extends Fragment implements OnClickListener {

    private static final String TAG = "CommunityFragment";
    private int mPreviousPos;
    private int mCurrentPos;
    private float mOffset;
    private float mDistance;
    private String propertyname = "translationX";
    private List<Fragment> mFragments;
    private FragmentManager mFragmentManager;
    private Context mContext;

    private ImageView iv_Menu;
    private ImageView iv_Topic;
    private ImageView iv_Post;
    private View view_Divide;
    private ViewPager vp_Container;
    private PtrClassicFrameLayout ptr_Refresh;
    private RadioButton rb_article;
    private RadioButton rb_item;
    private int times;


    public CommunityFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragments();
    }


    //初始化碎片
    private void initFragments() {
        mFragments = new ArrayList<>();
        mFragments.add(new ArticleFragment());
        mFragments.add(new ItemFragment());
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_community, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化控件
        initView(view);
        //初始化位置数据
        initData();
        //设置初始状态
        ObjectAnimator.ofFloat(view_Divide, "translationX", 0,200).setDuration(200).start();
        setState(mPreviousPos);
        //设置监听事件
        setListener();
        //mFragmentManager赋值这一步必须在onViewCreated周期中获得才可以不然回报下面这个错
        // NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction  android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
        mFragmentManager = getActivity().getSupportFragmentManager();
        vp_Container.setAdapter(new FragmentAdapter(mFragmentManager, mFragments));
        vp_Container.addOnPageChangeListener(new MyOnPageChangeListener());

    }


    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view) {
        iv_Menu = (ImageView) view.findViewById(R.id.iv_toolbar_menu);
        iv_Topic = (ImageView) view.findViewById(R.id.iv_toolbar_topic);
        iv_Post = (ImageView) view.findViewById(R.id.iv_toolbar_post);
        view_Divide = view.findViewById(R.id.view_community_divide);
        vp_Container = (ViewPager) view.findViewById(R.id.vp_community_container);
        ptr_Refresh = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_community_refresh);
        rb_article = (RadioButton) view.findViewById(R.id.rb_toolbar_article);
        rb_item = (RadioButton) view.findViewById(R.id.rb_toolbar_item);

    }

    //初始化位置数据
    private void initData() {
        mOffset = IApp.mScreenWidth - rb_article.getWidth();
        Log.i(TAG, "mOffset: " + mOffset);
        mDistance = rb_item.getX() - mOffset;
    }


    //设置状态
    private void setState(int position) {
        if (position == 0) {
            vp_Container.setCurrentItem(0);
            rb_article.setChecked(true);
            rb_item.setChecked(false);
            iv_Topic.setVisibility(View.VISIBLE);
            iv_Post.setVisibility(View.INVISIBLE);
        } else if (position == 1) {
            vp_Container.setCurrentItem(1);
            rb_article.setChecked(false);
            rb_item.setChecked(true);
            iv_Topic.setVisibility(View.INVISIBLE);
            iv_Post.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        iv_Menu.setOnClickListener(this);
        iv_Post.setOnClickListener(this);
        iv_Topic.setOnClickListener(this);
        rb_article.setOnClickListener(this);
        rb_item.setOnClickListener(this);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toolbar_menu:
                Toast.makeText(mContext, "menu", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_toolbar_post:
                Toast.makeText(mContext, "post", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_toolbar_topic:
                Toast.makeText(mContext, "topic", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rb_toolbar_article:
                setState(0);
                break;
            case R.id.rb_toolbar_item:
                setState(1);
                break;
        }
    }

    //自定义一个类实现OnPageChangeListener
    private class MyOnPageChangeListener implements OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setState(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


}
